home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / PostScript OSA / DropShell3 / AEHandlers.cpp next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  2.7 KB  |  104 lines

  1. #include <string>
  2.  
  3. #include "UAppleEventsMgr.h"
  4. #include "UExtractFromAEDesc.h"
  5. #include "AELib.h"
  6. #include "AEHandlers.h"
  7. #include "FileLib.h"
  8. #include "SendAEs.h"
  9.  
  10. using namespace std;
  11.  
  12. static Boolean GetAppProcess(OSType pCreator, ProcessSerialNumber *pProcess, FSSpec *pProcFile)
  13. {
  14.     ProcessInfoRec    sProcInfo;
  15.     Boolean            sFound = false;
  16.  
  17.     if (true /* gsHasProcessManager */) {
  18.         pProcess->highLongOfPSN = 0;
  19.         pProcess->lowLongOfPSN = kNoProcess;
  20.         sProcInfo.processInfoLength = sizeof(ProcessInfoRec);
  21.         sProcInfo.processName = nil;
  22.         sProcInfo.processAppSpec = pProcFile;
  23.         while ((!sFound) && (GetNextProcess(pProcess) == noErr)) {
  24.             if (GetProcessInformation(pProcess, &sProcInfo) == noErr) {
  25.                 if (sProcInfo.processSignature == pCreator) {
  26.                     if ((sProcInfo.processType == 'APPL') || 
  27.                         (sProcInfo.processType == 'appe') || 
  28.                         (sProcInfo.processType == 'appc'))
  29.                         sFound = true;
  30.                 }
  31.             }
  32.         }
  33.         if (!sFound) {
  34.             pProcess->highLongOfPSN = 0;
  35.             pProcess->lowLongOfPSN = kNoProcess;
  36.         }
  37.     }
  38.     return(sFound);
  39. }
  40.  
  41. static void BringAppToFront( OSType pCreator )
  42. {
  43.     ProcessSerialNumber pProcess;
  44.     FSSpec                pProcFile;
  45.     OSErr                iErr;
  46.  
  47.     if ( GetAppProcess( pCreator, &pProcess, &pProcFile ) ) {
  48.         iErr = SetFrontProcess( &pProcess );
  49.     }
  50. }
  51.  
  52.  
  53. pascal OSErr HandleDoScript ( AppleEvent *theAppleEvent, AppleEvent *reply, long handlerRefcon )
  54. {
  55. #pragma unused ( reply, handlerRefcon )
  56.  
  57.     OSErr err= noErr;
  58.     
  59.     try {
  60.         // get the PS data
  61.         string    psData;
  62.         StAEDescriptor    fdfDesc(*theAppleEvent, keyDirectObject);
  63.         UExtractFromAEDesc::TheString(fdfDesc, psData);
  64.     
  65.         // save it out to a file
  66.         FSSpec    psOutFile;
  67.         short    fRefNum; 
  68.         OSErr    err;
  69.         err = FSMakeFSSpec( 0, 0, "\ppsTemp.ps", &psOutFile );
  70.         err = FSpCreate( &psOutFile, 'R*ch', 'TEXT', smSystemScript );
  71.         if ( !err ) {
  72.             err = FSpOpenDF( &psOutFile, fsRdWrPerm, &fRefNum );
  73.             if ( !err ) {
  74.                 long    count = psData.length();
  75.                 FSWrite( fRefNum, &count, psData.c_str() );
  76.                 FSClose( fRefNum );
  77.             }
  78.         }
  79.         
  80.         // tell Distiller to process it (to a specific location)
  81.         SendEventToProc ( &psOutFile, kCoreEventClass, kAEOpenDocuments, 'DSTL' );
  82.         
  83.         // tell Acrobat to open the PDF document
  84.         //     once it exists (and is no longer busy) of course!
  85.         //    and then bring Acro to front
  86.         EventRecord    theEvent;
  87.         FSSpec    pdfOutFile;
  88.         err = FSMakeFSSpec( 0, 0, "\ppsTemp.pdf", &pdfOutFile );
  89.         while ( !FileExists( &pdfOutFile ) )    WaitNextEvent( 0, &theEvent, 0, NULL );
  90.         while ( FSpIsFileBusy( &pdfOutFile ) )    WaitNextEvent( 0, &theEvent, 0, NULL );
  91.         SendEventToProc ( &pdfOutFile, kCoreEventClass, kAEOpenDocuments, 'CARO' );
  92.         BringAppToFront( 'CARO' );
  93.  
  94.         // delete the temp file
  95.         FSpDelete( &psOutFile );
  96.     }
  97.     
  98.     catch (ExceptionCode inErr) {
  99.         err = inErr;
  100.     }
  101.     
  102.     return err;
  103. }
  104.